package com.xiaoran.robot.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xiaoran.robot.entity.WsCos;
import com.xiaoran.robot.service.WsCosService;
import com.xiaoran.robot.service.impl.WsCosServiceImpl;
import com.xiaoran.robot.utils.HttpConnectionUtils;
import com.xiaoran.robot.utils.StringUtils;
import com.xiaoran.robot.utils.YamlReader;
import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * 原神 cos版块图片采集
 */
@Component
public class CosTask {
    private String baseApi = "https://bbs-api.mihoyo.com/post/wapi/getForumPostList?forum_id=49&gids=2&is_good=false&is_hot=true&page_size=20&last_id=";
    private String cosPath = YamlReader.instance.getValueByKey("mirai.cosPath").toString();
    private static CosTask cosTask;
    @Autowired
    WsCosService wsCosService;

    @PostConstruct
    public void init(){
        cosTask = this;
        cosTask.wsCosService = this.wsCosService;
    }
    public List<String> getImgList() {
        HttpConnectionUtils.trustEveryone();
        for (int i = 100; i<=150; i++) {
            try {
                Document document = Jsoup.connect(baseApi + String.valueOf(i)).ignoreContentType(true).get();
                String jsonData = document.text();
                JSONObject data = StringUtils.toJson(jsonData);
                String post = data.getString("data");
                String postList = StringUtils.toJson(post).getString("list");
                JSONArray items = JSON.parseArray(postList);
                List<String> imageList = new ArrayList<>();
                List<WsCos> dataList = new ArrayList<>();
                for (Object item : items) {
                    JSONObject itemContent = StringUtils.toJson(StringUtils.toJson(item.toString()).getString("post"));
                    Integer post_id = Integer.parseInt(itemContent.getString("post_id"));
                    String subject = itemContent.getString("subject");
                    String images = itemContent.getString("images");
                    StringBuilder itemUrls = new StringBuilder();
                    //保存图片
                    String[] urls = images.replace("[", "").replace("]", "").split(",");
                    for (String url : urls) {
                        imageList.add(url.replaceAll("\"", ""));
                        itemUrls.append(url.replaceAll("\"", "")).append("|");
                    }
                    WsCos wsCos = new WsCos(post_id, subject, itemUrls.toString());
                    dataList.add(wsCos);
                }
//                saveImg(imageList);
                saveData(dataList);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public void saveImg(List<String> urlList) {
        HttpConnectionUtils.trustEveryone();
        for (String s : urlList) {
            String filename = s.substring(s.lastIndexOf("/") + 1);
            try {
                FileUtils.copyURLToFile(new URL(s), new File(cosPath + filename));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public void saveData(List<WsCos> datalist){
        cosTask.wsCosService.insertList(datalist);
    }
}
